local jass = require 'jass.common'
local dbg  = require 'jass.debug'
local ac_timer = ac.timer
local ac_alltimers = ac.alltimers

-- 注意:游戏初始化阶段显示无效 --
-- 最多同时能显示4个,超了则会看不见
local timer = {}
setmetatable(timer,timer)

-- 存放所有计时器
timer.alltimers = {}

-- 如果只有一个参数则不是中心计时器
ac.timer = function(name,...)
    local n = select('#',...)
    if n > 0 then 
        if ac_timer then 
            return ac_timer(name,...)
        end
    end 
    return timer.create(name)
end

-- 计时器到期
function timer.callback()
    local handle = jass.GetExpiredTimer()
    local t = timer.alltimers[handle]
    if t and t.on_timer then 
        t.on_timer(t) 
    end 
end 

-- 输出
function timer:__tostring()
    return ('[timer2] @%q #(line:%d)'):format(self._src,self._line)
end

-- 创建计时器
function timer.create(title)
    local handle = jass.CreateTimer()
    dbg.handle_ref(handle)
    local dialog = jass.CreateTimerDialog(handle)
    dbg.handle_ref(dialog)
    local obj = {}
    obj.title = title
    obj.gchash = handle
    obj.handle = handle
    obj.dialog = dialog
    dbg.gchash(obj,handle)
    setmetatable(obj,timer)
    timer.alltimers[handle] = obj
    return obj:init()
end 

local mt = {}
timer.__index = mt 

-- 结构
mt.type = 'timerdialog'

-- 标题
mt.title = '倒计时'

-- 句柄
mt.handle = jass.ConvertUnitState(0)

-- 对话框句柄
mt.dialog = jass.ConvertUnitState(0)

-- 初始化
function mt:init()
    jass.TimerDialogSetTitle(self.dialog,self.title)
    jass.TimerDialogDisplay(self.dialog,false)
    ac_alltimers[self] = true
    return self
end 

-- 销毁
function mt:remove()
    if self.removed then return end
    self.removed = true 
    local handle = self.handle 
    local dialog = self.dialog 
    jass.PauseTimer(handle)
    jass.TimerDialogDisplay(dialog,false)
    jass.DestroyTimerDialog(dialog)
    jass.DestroyTimer(handle)
    dbg.handle_unref(dialog)
    dbg.handle_unref(handle)
    timer.alltimers[handle] = nil
    ac_alltimers[self] = nil 
    self.dialog = nil 
    self.handle = nil 
end 

-- 注册
function mt:start(time, ...)
    if self.removed then return end
    local n = select('#',...)
    local loop, on_timer
    if n == 2 then 
        loop, on_timer = ...
    else 
        on_timer = ... 
    end
    self.is_loop = loop and true or false 
    self.on_timer = on_timer
    jass.PauseTimer(self.handle)
    jass.TimerStart(self.handle,time,loop and true or false, timer.callback)
    return self
end

-- 设置标题
function mt:set_title(title)
    self.title = title 
    jass.TimerDialogSetTitle(self.dialog,self.title)
end

-- 恢复
function mt:resume()
    jass.ResumeTimer(self.handle)
end 

-- 暂停
function mt:pause()
    jass.PauseTimer(self.handle)
end

-- 获取剩余时间
function mt:get_remaining()
    return jass.TimerGetRemaining(self.handle)
end

-- 设置剩余时间
function mt:set_remaining(time)
    jass.PauseTimer(self.handle)
    jass.TimerStart(self.handle, time, self.is_loop and true or false, timer.callback)
end

-- 显示计时器窗口
function mt:show(player)
    jass.TimerDialogSetTitle(self.dialog,self.title)
    if player then
        if player:is_self() then 
            jass.TimerDialogDisplay(self.dialog,true)
        end
    else
        jass.TimerDialogDisplay(self.dialog,true)
    end
end 

-- 隐藏计时器窗口
function mt:hide(player)
    if player and player:is_self() then 
        jass.TimerDialogDisplay(self.dialog,false)
    else
        jass.TimerDialogDisplay(self.dialog,false)
    end
end

return timer